﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bronze.XXT.PhoneServer
{
    public class MsgAttribute : Attribute
    {
        public string func_no
        { get; set; }
        /// <summary>
        /// 是否为话机向服务器
        /// </summary>
        public bool isRequest
        { get; set; }

        public MsgAttribute(string func_no, bool isRequest)
        {
            this.func_no = func_no;
            this.isRequest = isRequest;
        }

        private static Dictionary<Type, MsgAttribute> TypeMsgAttributeCache = new Dictionary<Type, MsgAttribute>();
        public static MsgAttribute GetData(Type type)
        {
        	MsgAttribute attr =null;
        	if (TypeMsgAttributeCache.ContainsKey(type)) {
        		attr = TypeMsgAttributeCache[type];
        	}
            
            if (attr == null)
            {
                var attrs = type.GetCustomAttributes(false);
                if (attrs != null && attrs.Length > 0)
                {
                    foreach (var item in attrs)
                    {
                        if (item.GetType() == typeof(MsgAttribute))
                        {
                            attr = item as MsgAttribute;
                            TypeMsgAttributeCache[type] = attr;
                            break;
                        }
                    }
                }
            }
            return attr;
        }

        public static MsgAttribute GetData<T>() where T : MessageBody
        {
            var type = typeof(T);
            return GetData(type);
        }
    }
}
